import { makeAutoObservable } from 'mobx'
import { getTestEvals, getTestEvalsRecordView, testEvalsSave } from './api'
import type { TestEvalRes, TestEvalResult } from './interface'
import { history } from 'umi'

class Store {
    quesInfo: TestEvalRes | Record<string, any> = {} /** 测评信息 */
    current: number = 1 /** 当前题目 */
    answerData: Record<string, any> = {} /** 答题结果 */
    resultInfo: TestEvalResult | Record<string, any> = {} /** 结果信息 */

    constructor() {
        makeAutoObservable(this)
    }

    /** 获取测评 */
    getTest = (id: string) => {
        getTestEvals(id).then(res => {
            this.quesInfo = res.data
        })
    }
    /** 勾选答案 */
    selectedAnswer = (id: string, value: string) => {
        this.answerData[id] = value
    }
    /** 下一题 */
    nextQues = () => {
        this.current = this.current + 1
    }
    /** 提交测评结果 */
    submitQues = (id: string) => {
        testEvalsSave({ eval_id: id, questions: Object.keys(this.answerData).map(item => ({ id: item, option_id: this.answerData[item] })) }).then(res => {
            history.replace(`/evaluation/result/${res.data?.id}`)
        })
    }

    /** 获取测评结果 */
    getResultById = (id: string) => {
        getTestEvalsRecordView(id).then(res => {
            this.resultInfo = res.data
        })
    }
}
export default Store
